package com.towexin.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

/**
 * TODO
 * Template模板工具
 *
 * @author Towexin
 * @version 1.0
 * @date 2021/4/12 12:04
 */

public class TemplateFileTool {
	private static final Logger logger = LoggerFactory.getLogger(TemplateFileTool.class);

	/**
	 * 创建HTML
	 * @param title html文件title
	 * @param url	html文件路径
	 * @return	Boolean
	 */
	public static boolean createHtml(String title, String url) {
		PrintWriter writer = null;
		try {
			String htmlPath = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "templates/").getPath()
					.replace("target\\classes", "src\\main\\resources") + "\\"
					+ url.substring(0, url.length() - ("/" + url.split("/")[url.split("/").length - 1]).length())
							.replace("/", "\\");

			// 1. 创建模板解析器 并设置相关属性
			ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
			// 配置解析器模板存储文件夹路径
			resolver.setPrefix("template-model/");
			// 配置解析器模板文件后缀
			resolver.setSuffix(".html");

			TemplateEngine templateEngine = new TemplateEngine();

			templateEngine.setTemplateResolver(resolver);

			// 2. 模板上下文 主要存储Model参数
			Context context = new Context();
			context.setVariable("title", title);

			if (!htmlPath.contains("page")) {
				return Boolean.TRUE;
			}

			// 3. 创建输出文件
			File fileDir = new File(htmlPath);
			if (!fileDir.isDirectory()) {
				fileDir.mkdirs();
				logger.info("创建目录");
			}

			File folder = new File(htmlPath, url.split("/")[url.split("/").length - 1] + ".html");
			// 如果文件不存在,直接创建
			if (!folder.exists()) {
				folder.createNewFile();
				// 5. 获取输出目标文件输出流
				writer = new PrintWriter(folder, StandardCharsets.UTF_8);

				// 6. 生成静态模板参数1:template模板名称 参数2:上下文对象 参数3:目标文件输出流
				templateEngine.process("model", context, writer);
				logger.info("创建文件");
			}
			return Boolean.TRUE;
		} catch (IOException e) {
			e.printStackTrace();
			return Boolean.FALSE;
		} finally {
			// flush输出流并关闭
			if (writer != null) {
				writer.flush();
				writer.close();
			}
		}
	}

	/**
	 * 删除HTML
	 * @param url	html文件路径
	 * @return	Boolean
	 */
	public static boolean deleteHtml(String url) {
		try {
			String htmlPath = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "templates/").getPath()
					.replace("target\\classes", "src\\main\\resources") + "\\"
					+ url.substring(0, url.length() - ("/" + url.split("/")[url.split("/").length - 1]).length())
							.replace("/", "\\");

			File folder = new File(htmlPath, url.split("/")[url.split("/").length - 1] + ".html");
			if (folder.exists()) {
				folder.delete();
				logger.info("删除文件");
			}

			File fileDir = new File(htmlPath);
			if (fileDir.isDirectory() && fileDir.length() == 0) {
				fileDir.delete();
				logger.info("删除目录");
			}

			return Boolean.TRUE;
		} catch (IOException e) {
			e.printStackTrace();
			return Boolean.FALSE;
		}
	}
}
